﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class ybqy_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {//填充第一级
            DataView dv = drv("SELECT CODE, NAME FROM CODE_TRADE WHERE ({ fn LENGTH(RTRIM(CODE)) } = 1) ORDER BY CODE");

            foreach (DataRowView datav in dv)
            {
                TreeNode tn = new TreeNode();
                tn.Text = datav["NAME"].ToString();
                tn.Value = datav["CODE"].ToString();
                tn.Expanded = false;
                TreeView1.Nodes.Add(tn);
                addchildnodes(tn);
            }

        }
    }
    private void addchildnodes(TreeNode tn)
    {
        DataView dv = drv("SELECT CODE, NAME FROM CODE_TRADE WHERE (SUBSTRING(CODE, 1, 1) = '" + tn.Value + "') AND ({ fn LENGTH(RTRIM(CODE)) } = 3) ORDER BY CODE");
        foreach (DataRowView datav in dv)
        {
            if (datav["NAME"].ToString() != null)
            {
                TreeNode ctn = new TreeNode();
                ctn.Text = datav["NAME"].ToString();
                ctn.Value = datav["CODE"].ToString();
                ctn.Expanded = false;
                tn.ChildNodes.Add(ctn);
                addchildnodes1(ctn);

            }

        }
    }
    private void addchildnodes1(TreeNode tn)
    {
        DataView dv = drv("SELECT CODE, NAME FROM CODE_TRADE WHERE (SUBSTRING(CODE, 1, 3) = '" + tn.Value + "') AND ({ fn LENGTH(RTRIM(CODE)) } = 4) ORDER BY CODE");
        foreach (DataRowView datav in dv)
        {
            if (datav["NAME"].ToString() != null)
            {
                TreeNode ctn = new TreeNode();
                ctn.Text = datav["NAME"].ToString();
                ctn.Value = datav["CODE"].ToString();
                ctn.Expanded = false;
                tn.ChildNodes.Add(ctn);
                addchildnodes2(ctn);

            }

        }
    }
    private void addchildnodes2(TreeNode tn)
    {
        DataView dv = drv("SELECT CODE, NAME FROM CODE_TRADE WHERE (SUBSTRING(CODE, 1, 4) = '" + tn.Value + "') AND ({ fn LENGTH(RTRIM(CODE)) } = 5) ORDER BY CODE");
        foreach (DataRowView datav in dv)
        {
            if (datav["NAME"].ToString() != null)
            {
                TreeNode ctn = new TreeNode();
                ctn.Text = datav["NAME"].ToString();
                ctn.Value = datav["CODE"].ToString();
                ctn.Expanded = false;
                tn.ChildNodes.Add(ctn);
                //addchildnodes1(ctn);

            }

        }
    }

    private DataView drv(string query)
    {
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();
        string str = query;
        SqlDataAdapter sda = new SqlDataAdapter(str, sqlcon);
        DataSet ds = new DataSet();
        sda.Fill(ds, "tree");
        sqlcon.Close();
        return ds.Tables["tree"].DefaultView;
        
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        string str = "";
        if (TreeView1.CheckedNodes.Count > 0)
        {
            foreach (TreeNode tn in TreeView1.CheckedNodes)
            {
                if (tn.ChildNodes.Count > 0)
                {
                    foreach (TreeNode ctn in tn.ChildNodes)
                    {
                        if (ctn.ChildNodes.Count > 0)
                        {
                            foreach (TreeNode dtn in ctn.ChildNodes)
                            {
                                if (dtn.ChildNodes.Count > 0)
                                {
                                    foreach (TreeNode etn in dtn.ChildNodes)
                                    {
                                        if (etn.ChildNodes.Count > 0)
                                        {
                                            foreach (TreeNode ftn in etn.ChildNodes)
                                            {

                                            }
                                        }
                                        else
                                        {
                                            str = "update  a01101 set TRADE_CODE='" + etn.Value + "' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];

                                        }

                                    }
                                }
                                else
                                {
                                    str = "update  a01101 set TRADE_CODE='" + dtn.Value + "' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];

                                }

                            }
                        }
                        else
                        {
                            str = "update  a01101 set TRADE_CODE='" + ctn.Value + "' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];

                        }

                    }
                }
                else
                {
                    str = "update  a01101 set TRADE_CODE='" + tn.Value + "' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];

                }
            }
        }
        str = "update  a01101 set TRADE_CODE='" + Label3.Text + "' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
        if (str!="")
        {
        SqlCommand sqlcmd = new SqlCommand(str, sqlcon);
        sqlcmd.ExecuteNonQuery();
        sqlcon.Close();    
        }
        Response.Redirect("1-1.aspx");

    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        Label3.Text = TreeView1.SelectedNode.Value;
        Label4.Text = TreeView1.SelectedNode.Text;

    }
}